/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ez2find.backing;

import com.ez2find.manager.CommunityCategorieManagerLocal;
import com.ez2find.manager.EventCategorieManagerLocal;
import com.ez2find.manager.GigCategorieManagerLocal;
import com.ez2find.manager.HousCategorieManagerLocal;
import com.ez2find.manager.JobCategorieManagerLocal;
import com.ez2find.manager.PersonalCategorieManagerLocal;
import com.ez2find.manager.SaleCategorieManagerLocal;
import com.ez2find.manager.ServiceCategorieManagerLocal;
import com.ez2find.model.CategoriaComunidad;
import com.ez2find.model.CategoriaEvento;
import com.ez2find.model.CategoriaTemporal;
import com.ez2find.model.CategoriaInmobiliario;
import com.ez2find.model.CategoriaTrabajo;
import com.ez2find.model.CategoriaPersonal;
import com.ez2find.model.CategoriaVenta;
import com.ez2find.model.CategoriaServicio;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 *
 * @author Owner
 */
public class CategorieBean {

    private long categorieId;
    private String categoryName;
    private boolean newCategory;
    private boolean editCategory;
    private int belongsTo;
    private boolean communityOpened;
    private boolean eventOpened;
    private boolean gigOpened;
    private boolean housOpened;
    private boolean jobOpened;
    private boolean personalOpened;
    private boolean saleOpened;
    private boolean serviceOpened;
    @EJB
    private JobCategorieManagerLocal ejbJobCategorieManager;
    @EJB
    private HousCategorieManagerLocal ejbHousCategorieManager;
    @EJB
    private SaleCategorieManagerLocal ejbSaleCategorieManager;
    @EJB
    private GigCategorieManagerLocal ejbGigCategorieManager;
    @EJB
    private ServiceCategorieManagerLocal ejbServiceCategorieManager;
    @EJB
    private CommunityCategorieManagerLocal ejbCommunityCategorieManager;
    @EJB
    private PersonalCategorieManagerLocal ejbPersonalCategorieManager;
    @EJB
    private EventCategorieManagerLocal ejbEventCategorieManager;
    
    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public boolean getNewCategory() {
        return newCategory;
    }

    public void setNewCategory(boolean newCategory) {
        this.newCategory = newCategory;
    }

    public String save() {
        System.out.println("Saving changes...");
        System.out.println("Editando categoria ?..." + getEditCategory() + belongsTo);
        if (!editCategory) {
            try{
            if (belongsTo == 4) {
                CategoriaInmobiliario hc = new CategoriaInmobiliario();
                hc.setCategorie(categoryName);
                ejbHousCategorieManager.create(hc);
                setHousOpened(true);
                categoryName=null;
                newCategory=false;
                FacesMessage msg = new FacesMessage("Category created succesfully.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
            if (belongsTo == 1) {
                CategoriaComunidad cc = new CategoriaComunidad();
                cc.setCategorie(categoryName);
                ejbCommunityCategorieManager.create(cc);
                setCommunityOpened(true);
                categoryName=null;
                newCategory=false;
                FacesMessage msg = new FacesMessage("Category created succesfully.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
            if (belongsTo == 2) {
                CategoriaEvento ec = new CategoriaEvento();
                ec.setCategorie(categoryName);
                ejbEventCategorieManager.create(ec);
                setEventOpened(true);
                categoryName=null;
                newCategory=false;
                FacesMessage msg = new FacesMessage("Category created succesfully.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
            if (belongsTo == 3) {
                CategoriaTemporal gc = new CategoriaTemporal();
                gc.setCategorie(categoryName);
                ejbGigCategorieManager.create(gc);
                setGigOpened(true);
                categoryName=null;
                newCategory=false;
                FacesMessage msg = new FacesMessage("Category created succesfully.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
            if (belongsTo == 5) {
                CategoriaTrabajo jc = new CategoriaTrabajo();
                jc.setCategorie(categoryName);
                ejbJobCategorieManager.create(jc);
                setJobOpened(true);
                categoryName=null;
                newCategory=false;
                FacesMessage msg = new FacesMessage("Category created succesfully.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
            if (belongsTo == 6) {
                CategoriaPersonal pc = new CategoriaPersonal();
                pc.setCategorie(categoryName);
                ejbPersonalCategorieManager.create(pc);
                setPersonalOpened(true);
                categoryName=null;
                newCategory=false;
                FacesMessage msg = new FacesMessage("Category created succesfully.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
            if (belongsTo == 7) {
                CategoriaVenta sc = new CategoriaVenta();
                sc.setCategorie(categoryName);
                ejbSaleCategorieManager.create(sc);
                setSaleOpened(true);
                categoryName=null;
                newCategory=false;
                FacesMessage msg = new FacesMessage("Category created succesfully.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
            if (belongsTo == 8) {
                CategoriaServicio sec = new CategoriaServicio();
                sec.setCategorie(categoryName);
                ejbServiceCategorieManager.create(sec);
                setServiceOpened(true);
                categoryName=null;
                newCategory=false;
                FacesMessage msg = new FacesMessage("Category created succesfully.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
            }catch(Exception ex){
                FacesMessage msg = new FacesMessage("Can not create category");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
            }
        } else {
            try{
            if (belongsTo == 4) {
                CategoriaInmobiliario hc = ejbHousCategorieManager.find(categorieId);
                hc.setCategorie(categoryName);
                ejbHousCategorieManager.update(hc);
                categoryName=null;
                newCategory=false;
                editCategory=false;
                FacesMessage msg = new FacesMessage("Category updated succesfully.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
            if (belongsTo == 1) {
                CategoriaComunidad cc = ejbCommunityCategorieManager.find(categorieId);
                cc.setCategorie(categoryName);
                ejbCommunityCategorieManager.update(cc);
                categoryName=null;
                newCategory=false;
                editCategory=false;
                FacesMessage msg = new FacesMessage("Category updated succesfully.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
            if (belongsTo == 2) {
                CategoriaEvento ec = ejbEventCategorieManager.find(categorieId);
                ec.setCategorie(categoryName);
                ejbEventCategorieManager.update(ec);
                categoryName=null;
                newCategory=false;
                editCategory=false;
                FacesMessage msg = new FacesMessage("Category updated succesfully.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
            if (belongsTo == 3) {
                CategoriaTemporal gc = ejbGigCategorieManager.find(categorieId);
                gc.setCategorie(categoryName);
                ejbGigCategorieManager.update(gc);
                categoryName=null;
                newCategory=false;
                editCategory=false;
                FacesMessage msg = new FacesMessage("Category updated succesfully.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
            if (belongsTo == 5) {
                CategoriaTrabajo jc = ejbJobCategorieManager.find(categorieId);
                jc.setCategorie(categoryName);
                ejbJobCategorieManager.update(jc);
                categoryName=null;
                newCategory=false;
                editCategory=false;
                FacesMessage msg = new FacesMessage("Category updated succesfully.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
            if (belongsTo == 6) {
                CategoriaPersonal pc = ejbPersonalCategorieManager.find(categorieId);
                pc.setCategorie(categoryName);
                ejbPersonalCategorieManager.update(pc);
                categoryName=null;
                newCategory=false;
                editCategory=false;
                FacesMessage msg = new FacesMessage("Category updated succesfully.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
            if (belongsTo == 7) {
                CategoriaVenta sc = ejbSaleCategorieManager.find(categorieId);
                sc.setCategorie(categoryName);
                ejbSaleCategorieManager.update(sc);
                categoryName=null;
                newCategory=false;
                editCategory=false;
                FacesMessage msg = new FacesMessage("Category updated succesfully.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
            if (belongsTo == 8) {
                CategoriaServicio sec = ejbServiceCategorieManager.find(categorieId);
                sec.setCategorie(categoryName);
                ejbServiceCategorieManager.update(sec);
                categoryName=null;
                newCategory=false;
                editCategory=false;
                FacesMessage msg = new FacesMessage("Category updated succesfully.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
            }catch(Exception ex){
                FacesMessage msg = new FacesMessage("Can not update category");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
            }
        }

        return null;
    }

    public int getBelongsTo() {
        return belongsTo;
    }

    public void setBelongsTo(int belongsTo) {
        this.belongsTo = belongsTo;
    }
/*
    public Object findByName(String name, int n) {
        Object ans = null;
        if (n == 4) {
            for (CategoriaInmobiliario hc : ejbHousCategorieManager.listAll()) {
                if (name.matches(hc.getCategorie())) {
                    ans = hc;
                }
            }
            return ans;
        }
        if (n == 1) {
            for (CategoriaComunidad cc : ejbCommunityCategorieManager.listAll()) {
                if (name.matches(cc.getCategorie())) {
                    ans = cc;
                }
            }
            return ans;
        }
        if (n == 2) {
            for (CategoriaEvento ec : ejbEventCategorieManager.listAll()) {
                if (name.matches(ec.getCategorie())) {
                    ans = ec;
                }
            }
            return ans;
        }
        if (n == 3) {
            for (CategoriaTemporal gc : ejbGigCategorieManager.listAll()) {
                if (name.matches(gc.getCategorie())) {
                    ans = gc;
                }
            }
            return ans;
        }
        if (n == 5) {
            for (CategoriaTrabajo jc : ejbJobCategorieManager.listAll()) {
                if (name.matches(jc.getCategorie())) {
                    ans = jc;
                }
            }
            return ans;
        }
        if (n == 6) {
            for (CategoriaPersonal pc : ejbPersonalCategorieManager.listAll()) {
                if (name.matches(pc.getCategorie())) {
                    ans = pc;
                }
            }
            return ans;
        }
        if (n == 7) {
            for (CategoriaVenta sc : ejbSaleCategorieManager.listAll()) {
                if (name.matches(sc.getCategorie())) {
                    ans = sc;
                }
            }
            return ans;
        }
        if (n == 8) {
            for (CategoriaServicio sec : ejbServiceCategorieManager.listAll()) {
                if (name.matches(sec.getCategorie())) {
                    ans = sec;
                }
            }
            return ans;
        }
        
        return ans;
    }
*/

    public void removeCommunityCategory() {
        try {
            ejbCommunityCategorieManager.delete(ejbCommunityCategorieManager.find(categorieId));
            categoryName=null;
            newCategory=false;
            editCategory=false;
            FacesMessage msg = new FacesMessage("Category deleted succesfully.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } catch (Exception ex) {
            categoryName=null;
            newCategory=false;
            editCategory=false;
            FacesMessage msg = new FacesMessage("Can not delete category");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
    
    public void removeEventCategory() {
        try {
            ejbEventCategorieManager.delete(ejbEventCategorieManager.find(categorieId));
            categoryName=null;
            newCategory=false;
            editCategory=false;
            categoryName=null;
            newCategory=false;
            editCategory=false;
            FacesMessage msg = new FacesMessage("Category deleted succesfully.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } catch (Exception ex) {
            categoryName=null;
            newCategory=false;
            editCategory=false;
            FacesMessage msg = new FacesMessage("Can not delete category");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
    public void removeGigCategory() {
        try {
            ejbGigCategorieManager.delete(ejbGigCategorieManager.find(categorieId));
            categoryName=null;
            newCategory=false;
            editCategory=false;
            FacesMessage msg = new FacesMessage("Category deleted succesfully.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } catch (Exception ex) {
            categoryName=null;
            newCategory=false;
            editCategory=false;
            FacesMessage msg = new FacesMessage("Can not delete category");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
    
    public void removeHousCategory() {
        try {
            ejbHousCategorieManager.delete(ejbHousCategorieManager.find(categorieId));
            categoryName=null;
            newCategory=false;
            editCategory=false;
            FacesMessage msg = new FacesMessage("Category deleted succesfully.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } catch (Exception ex) {
            categoryName=null;
            newCategory=false;
            editCategory=false;
            FacesMessage msg = new FacesMessage("Can not delete category");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
    
    public void removeJobCategory() {
        try {
            ejbJobCategorieManager.delete(ejbJobCategorieManager.find(categorieId));
            categoryName=null;
            newCategory=false;
            editCategory=false;
            FacesMessage msg = new FacesMessage("Category deleted succesfully.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } catch (Exception ex) {
            categoryName=null;
            newCategory=false;
            editCategory=false;
            FacesMessage msg = new FacesMessage("Can not delete category");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
    
    public void removePersonalCategory() {
        try {
            ejbPersonalCategorieManager.delete(ejbPersonalCategorieManager.find(categorieId));
            categoryName=null;
            newCategory=false;
            editCategory=false;
            FacesMessage msg = new FacesMessage("Category deleted succesfully.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } catch (Exception ex) {
            categoryName=null;
            newCategory=false;
            editCategory=false;
            FacesMessage msg = new FacesMessage("Can not delete category");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
    
    
    public void removeSaleCategory() {
        try {
            ejbSaleCategorieManager.delete(ejbSaleCategorieManager.find(categorieId));
            categoryName=null;
            newCategory=false;
            editCategory=false;
            FacesMessage msg = new FacesMessage("Category deleted succesfully.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } catch (Exception ex) {
            categoryName=null;
            newCategory=false;
            editCategory=false;
            FacesMessage msg = new FacesMessage("Can not delete category");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
    
    public void removeServiceCategory() {
        try {
            ejbServiceCategorieManager.delete(ejbServiceCategorieManager.find(categorieId));
            categoryName=null;
            newCategory=false;
            editCategory=false;
            FacesMessage msg = new FacesMessage("Category deleted succesfully.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } catch (Exception ex) {
            categoryName=null;
            newCategory=false;
            editCategory=false;
            FacesMessage msg = new FacesMessage("Can not delete category");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
    
    public boolean getHousOpened() {
        return housOpened;
    }

    public void setHousOpened(boolean housOpened) {
        this.housOpened = housOpened;
    }

    public boolean getEditCategory() {
        return editCategory;
    }

    public void setEditCategory(boolean editCategory) {
        this.editCategory = editCategory;
    }

    public boolean isCommunityOpened() {
        return communityOpened;
    }

    public void setCommunityOpened(boolean communityOpened) {
        this.communityOpened = communityOpened;
    }

    public boolean isEventOpened() {
        return eventOpened;
    }

    public void setEventOpened(boolean eventOpened) {
        this.eventOpened = eventOpened;
    }

    public boolean isGigOpened() {
        return gigOpened;
    }

    public void setGigOpened(boolean gigOpened) {
        this.gigOpened = gigOpened;
    }

    public boolean isJobOpened() {
        return jobOpened;
    }

    public void setJobOpened(boolean jobOpened) {
        this.jobOpened = jobOpened;
    }

    public boolean isPersonalOpened() {
        return personalOpened;
    }

    public void setPersonalOpened(boolean personalOpened) {
        this.personalOpened = personalOpened;
    }

    public boolean isSaleOpened() {
        return saleOpened;
    }

    public void setSaleOpened(boolean saleOpened) {
        this.saleOpened = saleOpened;
    }

    public boolean isServiceOpened() {
        return serviceOpened;
    }

    public void setServiceOpened(boolean serviceOpened) {
        this.serviceOpened = serviceOpened;
    }

    public long getCategorieId() {
        return categorieId;
    }

    public void setCategorieId(long categorieId) {
        this.categorieId = categorieId;
    }
}
